package com.jwds.digital.bios.business.service.health;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 调度规则工厂
 * @author hesong
 */
@Component
public class HealthScoreFactory implements InitializingBean {

    private static final Map<String, HealthScoreInterface> RULE_MAP = new ConcurrentHashMap<>();
    @Autowired
    private ApplicationContext context;

    @Override
    public void afterPropertiesSet() throws Exception {
        Map<String, HealthScoreInterface> beansMap = context.getBeansOfType(HealthScoreInterface.class);
        for (HealthScoreInterface value : beansMap.values()) {
            RULE_MAP.put(value.getDeductionCode(), value);
        }
    }

    public HealthScoreInterface getHandlerInstance(String ruleCode) {
        return RULE_MAP.get(ruleCode);
    }

    public Set<String> getRuleCodes() {
        return RULE_MAP.keySet();
    }
}
